package com.sdp.wx.controller;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.ApplicationContextUtils;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_army.entity.WxArmy;
import com.sdp.wx.db.wx_army.service.IWxArmyService;
import com.sdp.wx.db.wx_contract_detail.entity.WxContractDetail;
import com.sdp.wx.db.wx_contract_detail.service.IWxContractDetailService;
import com.sdp.wx.db.wx_repair_must_item.service.IWxRepairMustItemService;
import com.sdp.wx.db.wx_repair_must_wz.service.IWxRepairMustWzService;
import com.sdp.wx.db.wx_repair_vehicle.entity.WxRepairVehicle;
import com.sdp.wx.db.wx_repair_vehicle.service.IWxRepairVehicleService;
import com.sdp.wx.db.wx_repair_vehicle_item.entity.WxRepairVehicleItem;
import com.sdp.wx.db.wx_repair_vehicle_item.service.IWxRepairVehicleItemService;
import com.sdp.wx.db.wx_repair_vehicle_other_expense.entity.WxRepairVehicleOtherExpense;
import com.sdp.wx.db.wx_repair_vehicle_other_expense.service.IWxRepairVehicleOtherExpenseService;
import com.sdp.wx.db.wx_repair_vehicle_wz.service.IWxRepairVehicleWzService;
import com.sdp.wx.db.wx_sys_config.entity.WxSysConfig;
import com.sdp.wx.db.wx_sys_config.service.IWxSysConfigService;
import com.sdp.wx.db.wx_wz_output_detail.entity.WxWzOutputDetail;
import com.sdp.wx.db.wx_wz_output_detail.service.IWxWzOutputDetailService;
import com.sdp.wx.utils.WxConstants;
import com.sdp.wx.view.WxRepairMustItemView;
import com.sdp.wx.view.WxRepairMustWzView;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/WxRepairVehicleController")
public class WxRepairVehicleController {
	

	@Autowired
	private Query query;
	
	@Autowired
	private IWxRepairVehicleService iWxRepairVehicleService;
	
	@Autowired
	private IWxContractDetailService iWxContractDetailService;
	
	@Autowired
	private IWxArmyService iWxArmyService;
	
	@Autowired
	private IWxSysConfigService iWxSysConfigService;
	
	@Autowired
	private WxRepairCodeController wxRepairCodeController;
	
	@Autowired
	private IWxRepairMustItemService iWxRepairMustItemService;
	
	@Autowired
	private IWxRepairVehicleItemService iWxRepairVehicleItemService;
	
	@Autowired
	private IWxRepairMustWzService iWxRepairMustWzService;
	
	@Autowired
	private IWxRepairVehicleWzService iWxRepairVehicleWzService;
	
	@Autowired
	private IWxWzOutputDetailService iWxWzOutputDetailService;
	
	@Autowired
	private IWxRepairVehicleOtherExpenseService iWxRepairVehicleOtherExpenseService;
	
	@GetMapping(value = "/getById")
	public Result<?> getById(@RequestParam(name="keyId",required=true) String keyId) {
		return Result.ok(iWxRepairVehicleService.getById(keyId));
	}
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWxRepairVehicleService, WxRepairVehicle.class, dataCenter));
	}
	
	@GetMapping(value = "/getRepairInfoByRepairCode")
	public Result<?> getRepairInfoByRepairCode(@RequestParam(name="repairCode",required=true) String repairCode) {
		//查询维修项目信息
		List<WxRepairVehicleItem> itemList = iWxRepairVehicleItemService.queryWxRepairVehicleItemByRepairCode(repairCode);
		if(itemList==null || itemList.size()==0) {
			itemList = new ArrayList<>();
		}
		//查询器材信息
		List<WxWzOutputDetail> wzList = iWxWzOutputDetailService.queryWzOutputDetailByRepairCode(repairCode);
		if(wzList==null || wzList.size()==0) {
			wzList = new ArrayList<>();
		}
		
		//查询附件费信息
		List<WxRepairVehicleOtherExpense> expenseList = iWxRepairVehicleOtherExpenseService.queryRepairVehicleOtherExpenseByRepairCode(repairCode, WxConstants.是.getCode());
		if(expenseList==null || expenseList.size()==0) {
			expenseList = new ArrayList<>();
		}
		JSONObject obj = new JSONObject();
		obj.put("itemList", itemList);
		obj.put("wzList", wzList);
		obj.put("expenseList", expenseList);
		return Result.ok(obj);
	}

	/**
	 * 入厂交接保存
	 * @param repairVehicle
	 * @return
	 */
	@PostMapping(value = "/rcjjSave")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> rcjjSave(@RequestBody WxRepairVehicle repairVehicle){
		if(repairVehicle==null) {
			return Result.error("维修信息为空");
		}
		if(WxConstants.整车和总成维修合同.getCode().equals(repairVehicle.getContractType()) || WxConstants.工程装备维修合同.getCode().equals(repairVehicle.getContractType())) {
			if(!WxConstants.维修状态_已排计划.getCode().equals(repairVehicle.getRepairStatus())) {
				return Result.error("只有维修状态处于《已排计划》的车辆或者设备才能进行入厂交接保存");
			}
		}
		if(StringUtils.isEmpty(repairVehicle.getCreateTime())) {
			repairVehicle.setCreateTime(LocalDateTime.now());
		}
		if(StringUtils.isEmpty(repairVehicle.getHandoverDate())) {
			repairVehicle.setHandoverDate(LocalDate.now());
		}
		if(StringUtils.isEmpty(repairVehicle.getRepairCode())) {
			repairVehicle.setRepairCode(wxRepairCodeController.createRepairCode(WxConstants.流水号类型_维修单.getCode(),WxConstants.流水号前缀_维修单.getCode()));
		}
		setArmyAndOurFactoryInfo(repairVehicle);
		
		iWxRepairVehicleService.saveOrUpdate(repairVehicle);
		return Result.ok(repairVehicle); 
	}
	
	/**
	 * 入厂交接完成
	 * @param repairVehicle
	 * @return
	 */
	@PostMapping(value = "/rcjjFinish")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> rcjjFinish(@RequestBody WxRepairVehicle repairVehicle){
		Result<?> r = this.rcjjSave(repairVehicle);
		if(r==null || !r.isSuccess()) {
			return r;
		}
		repairVehicle.setRepairStatus(WxConstants.维修状态_入厂交接.getCode());
		iWxRepairVehicleService.saveOrUpdate(repairVehicle);
		
		//更新合同中车辆维修状态
		
		WxContractDetail wxContractDetail = iWxContractDetailService.getById(repairVehicle.getKeyId());
		if(wxContractDetail!=null) {
			wxContractDetail.setRepairStatus(WxConstants.维修状态_入厂交接.getCode());
			iWxContractDetailService.updateById(wxContractDetail);
		}
		return Result.ok(repairVehicle); 
	}
	
	
	
	/**
	 * 进厂检修保存
	 * @param repairVehicle
	 * @return
	 */
	@PostMapping(value = "/jcjxSave")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> jcjxSave(@RequestBody WxRepairVehicle repairVehicle){
		if(repairVehicle==null) {
			return Result.error("维修信息为空");
		}
		if(WxConstants.整车和总成维修合同.getCode().equals(repairVehicle.getContractType()) || WxConstants.工程装备维修合同.getCode().equals(repairVehicle.getContractType())) {
			if(!WxConstants.维修状态_入厂交接.getCode().equals(repairVehicle.getRepairStatus())) {
				return Result.error("只有维修状态处于《入厂交接》的车辆或者设备才能进行进厂检修保存");
			}
		}
		if(StringUtils.isEmpty(repairVehicle.getInFactoryCheckDate())) {
			repairVehicle.setInFactoryCheckDate(LocalDate.now());
		}
		iWxRepairVehicleService.updateById(repairVehicle);
		return Result.ok(repairVehicle); 
	}
	
	/**
	 * 进厂检修完成
	 * @param repairVehicle
	 * @return
	 */
	@PostMapping(value = "/jcjxFinish")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> jcjxFinish(@RequestBody WxRepairVehicle repairVehicle){
		Result<?> r =  ApplicationContextUtils.getBean(WxRepairVehicleController.class).jcjxSave(repairVehicle);
		if(r==null || !r.isSuccess()) {
			return r;
		}
		repairVehicle.setRepairStatus(WxConstants.维修状态_进厂检验.getCode());
		iWxRepairVehicleService.updateById(repairVehicle);
		
		//更新合同中车辆维修状态
		WxContractDetail wxContractDetail = iWxContractDetailService.getById(repairVehicle.getKeyId());
		if(wxContractDetail!=null) {
			wxContractDetail.setRepairStatus(WxConstants.维修状态_进厂检验.getCode());
			iWxContractDetailService.updateById(wxContractDetail);
		}
		
		
		//根据维修类型添加必修项目
		List<WxRepairMustItemView> mustItemList = iWxRepairMustItemService.queryRepairMustItemViewByRepairType(repairVehicle.getRepairType());
		if(mustItemList!=null && mustItemList.size()>0) {
			iWxRepairVehicleItemService.addMustRepairVehicleItem(mustItemList, repairVehicle);
		}
		
		//根据维修类型和撤销添加必换件
		List<WxRepairMustWzView> mustWzList = iWxRepairMustWzService.queryRepairMustWzViewByRepairTypeAndVehicleModel(repairVehicle.getRepairType(), repairVehicle.getVehicleModel());
		if(mustWzList!=null && mustWzList.size()>0) {
			iWxRepairVehicleWzService.addMustRepairVehicleWz(mustWzList,repairVehicle);
		}
		return Result.ok(repairVehicle); 
	}
	
	
	
	/**
	 * 工序调度保存
	 * @param repairVehicle
	 * @return
	 */
	@PostMapping(value = "/gxddSave")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> gxddSave(@RequestBody WxRepairVehicle repairVehicle){
		if(repairVehicle==null) {
			return Result.error("维修信息为空");
		}
		if(WxConstants.整车和总成维修合同.getCode().equals(repairVehicle.getContractType()) || WxConstants.工程装备维修合同.getCode().equals(repairVehicle.getContractType())) {
			if(!WxConstants.维修状态_进厂检验.getCode().equals(repairVehicle.getRepairStatus())) {
				return Result.error("只有维修状态处于《进厂检验》的车辆或者设备才能进行工序调度保存");
			}
		}
		if(StringUtils.isEmpty(repairVehicle.getScheduledSt())) {
			repairVehicle.setScheduledSt(LocalDateTime.now());
			repairVehicle.setScheduledSd(LocalDate.now());
		}
		//计算费用
		computeAmount(repairVehicle);
		
		iWxRepairVehicleService.updateById(repairVehicle);
		return Result.ok(repairVehicle); 
	}
	
	/**
	 * 工序调度完成
	 * @param repairVehicle
	 * @return
	 */
	@PostMapping(value = "/gxddFinish")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> gxddFinish(@RequestBody WxRepairVehicle repairVehicle){
		Result<?> r = ApplicationContextUtils.getBean(WxRepairVehicleController.class).gxddSave(repairVehicle);
		if(r==null || !r.isSuccess()) {
			return r;
		}
		
		//检验是否有未派工的项目
		if(!iWxRepairVehicleItemService.isDispatch(repairVehicle.getRepairCode())) {
			return Result.error("维修项目不存在或者有未派工的维修项目，请确认");
		}
		
		//检验是否有修理工进行领料但是没有器材出库信息的情况
		if(iWxRepairVehicleWzService.queryWxRepairVehicleWzCountByRepairCode(repairVehicle.getRepairCode())>0) {
			if(iWxWzOutputDetailService.queryWzOutputDetailCountByRepairCode(repairVehicle.getRepairCode())==0) {
				return Result.error("有维修工领料信息但是没有对应的器材出库信息，请先确认器材正确领用");
			}
		}
		
		
		//计算费用
		computeAmount(repairVehicle);
		
		repairVehicle.setRepairStatus(WxConstants.维修状态_工序调度.getCode());
		iWxRepairVehicleService.updateById(repairVehicle);
		
		//更新合同中车辆维修状态
		WxContractDetail wxContractDetail = iWxContractDetailService.getById(repairVehicle.getKeyId());
		if(wxContractDetail!=null) {
			wxContractDetail.setRepairStatus(WxConstants.维修状态_工序调度.getCode());
			iWxContractDetailService.updateById(wxContractDetail);
		}
		return Result.ok(repairVehicle); 
	}
	
	/**
	 * 计算费用
	 * @param repairVehicle
	 */
	private void computeAmount(WxRepairVehicle repairVehicle) {
		//计算所有费用
		repairVehicle.setHourAmount(iWxRepairVehicleItemService.queryHourAmount(repairVehicle.getRepairCode()));
		repairVehicle.setWzAmount(iWxWzOutputDetailService.queryWzAmountByRepairCode(repairVehicle.getRepairCode()));
		repairVehicle.setExtraAmount(iWxRepairVehicleOtherExpenseService.queryExtraAmount(repairVehicle.getRepairCode()));
		repairVehicle.setRealAmount(repairVehicle.getHourAmount().add(repairVehicle.getWzAmount()).add(repairVehicle.getExtraAmount()));
		repairVehicle.setArmyAmount(repairVehicle.getHourAmount().add(repairVehicle.getWzAmount()).add(iWxRepairVehicleOtherExpenseService.queryArmyExtraAmount(repairVehicle.getRepairCode())));
	}
	
	/**
	 * 出厂检验保存
	 * @param repairVehicle
	 * @return
	 */
	@PostMapping(value = "/ccjySave")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> ccjySave(@RequestBody WxRepairVehicle repairVehicle){
		if(repairVehicle==null) {
			return Result.error("维修信息为空");
		}
		if(WxConstants.整车和总成维修合同.getCode().equals(repairVehicle.getContractType()) || WxConstants.工程装备维修合同.getCode().equals(repairVehicle.getContractType())) {
			if(!WxConstants.维修状态_工序调度.getCode().equals(repairVehicle.getRepairStatus())) {
				return Result.error("只有维修状态处于《工序调度》的车辆或者设备才能进行出厂检验保存");
			}
		}
		if(StringUtils.isEmpty(repairVehicle.getOutFactoryCheckDate())) {
			repairVehicle.setOutFactoryCheckDate(LocalDate.now());
		}
		iWxRepairVehicleService.updateById(repairVehicle);
		return Result.ok(repairVehicle);
	}
	
	/**
	 * 出厂检验完成
	 * @param repairVehicle
	 * @return
	 */
	@PostMapping(value = "/ccjyFinish")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> ccjyFinish(@RequestBody WxRepairVehicle repairVehicle){
		
		Result<?> r = ApplicationContextUtils.getBean(WxRepairVehicleController.class).ccjySave(repairVehicle);
		if(r==null || !r.isSuccess()) {
			return r;
		}
		
		repairVehicle.setRepairStatus(WxConstants.维修状态_出厂检验.getCode());
		iWxRepairVehicleService.updateById(repairVehicle);
		
		//更新合同中车辆维修状态
		WxContractDetail wxContractDetail = iWxContractDetailService.getById(repairVehicle.getKeyId());
		if(wxContractDetail!=null) {
			wxContractDetail.setRepairStatus(WxConstants.维修状态_出厂检验.getCode());
			iWxContractDetailService.updateById(wxContractDetail);
		}
		return Result.ok(repairVehicle);
	}
	
	/**
	 * 检验交付保存
	 * @param repairVehicle
	 * @return
	 */
	@PostMapping(value = "/jyjfSave")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> jyjfSave(@RequestBody WxRepairVehicle repairVehicle){
		
		if(repairVehicle==null) {
			return Result.error("维修信息为空");
		}
		if(WxConstants.整车和总成维修合同.getCode().equals(repairVehicle.getContractType()) || WxConstants.工程装备维修合同.getCode().equals(repairVehicle.getContractType())) {
			if(!WxConstants.维修状态_出厂检验.getCode().equals(repairVehicle.getRepairStatus())) {
				return Result.error("只有维修状态处于《出厂检验》的车辆或者设备才能进行检验交付保存");
			}
		}
		
		if(StringUtils.isEmpty(repairVehicle.getDeliveryDate())) {
			repairVehicle.setDeliveryDate(LocalDate.now());
		}
		iWxRepairVehicleService.updateById(repairVehicle);
		return Result.ok(repairVehicle);
	}
	
	/**
	 * 检验交付完成
	 * @param repairVehicle
	 * @return
	 */
	@PostMapping(value = "/jyjfFinish")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> jyjfFinish(@RequestBody WxRepairVehicle repairVehicle){
		
		Result<?> r = ApplicationContextUtils.getBean(WxRepairVehicleController.class).jyjfSave(repairVehicle);
		if(r==null || !r.isSuccess()) {
			return r;
		}
		
		repairVehicle.setRepairStatus(WxConstants.维修状态_检验交付.getCode());
		iWxRepairVehicleService.updateById(repairVehicle);
		
		//更新合同中车辆维修状态
		WxContractDetail wxContractDetail = iWxContractDetailService.getById(repairVehicle.getKeyId());
		if(wxContractDetail!=null) {
			wxContractDetail.setRepairStatus(WxConstants.维修状态_检验交付.getCode());
			iWxContractDetailService.updateById(wxContractDetail);
		}
		return Result.ok(repairVehicle);
	}
	
	
	
	/**
	 * 小修添加车辆
	 * @param repairVehicle
	 * @return
	 */
	@PostMapping(value = "/minorRepairAdd")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> minorRepairAdd(@RequestBody WxRepairVehicle repairVehicle){
		if(repairVehicle==null) {
			return Result.error("维修信息为空");
		}
		repairVehicle.setCreateTime(LocalDateTime.now());
		repairVehicle.setRepairStatus(WxConstants.维修状态_入厂交接.getCode());
		repairVehicle.setHandoverDate(LocalDate.now());
		repairVehicle.setRepairCode(wxRepairCodeController.createRepairCode(WxConstants.流水号类型_维修单.getCode(),WxConstants.流水号前缀_维修单.getCode()));
		setArmyAndOurFactoryInfo(repairVehicle);	
		iWxRepairVehicleService.save(repairVehicle);
		return Result.ok(repairVehicle); 
	}
	
	
	/**
	 * 技术支援保障维修添加
	 * @param repairVehicle
	 * @return
	 */
	@PostMapping(value = "/jszybzAdd")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> jszybzAdd(@RequestBody WxRepairVehicle repairVehicle){
		if(repairVehicle==null) {
			return Result.error("维修信息为空");
		}
		repairVehicle.setCreateTime(LocalDateTime.now());
		repairVehicle.setRepairStatus(WxConstants.维修状态_进厂检验.getCode());
		repairVehicle.setHandoverDate(LocalDate.now());
		repairVehicle.setRepairCode(wxRepairCodeController.createRepairCode(WxConstants.流水号类型_维修单.getCode(),WxConstants.流水号前缀_维修单.getCode()));
		setArmyAndOurFactoryInfo(repairVehicle);	
		iWxRepairVehicleService.save(repairVehicle);
		return Result.ok(repairVehicle); 
	}
	
	/**
	 * 设置送修单位和维修厂信息
	 * @param repairVehicle
	 */
	private void setArmyAndOurFactoryInfo(WxRepairVehicle repairVehicle) {
		//设置送修单位信息
		if(StringUtils.isEmpty(repairVehicle.getArmyName())) {
			WxArmy army = iWxArmyService.queryWxArmyByCode(repairVehicle.getSendRepairOrg());
			if(!StringUtils.isEmpty(army)) {
				repairVehicle.setArmyName(army.getArmyName());
				repairVehicle.setArmyAddress(army.getArmyAddress());
				repairVehicle.setArmyPhone(army.getArmyPhone());
				repairVehicle.setArmAcBank(army.getAcBank());
				repairVehicle.setArmAcName(army.getAcName());
				repairVehicle.setArmAcNo(army.getAcNo());
			}
		}
		
		//设置承接单位信息
		if(StringUtils.isEmpty(repairVehicle.getOurFactoryName())) {
			List<String> paramCodes = new ArrayList<>();
			paramCodes.add(WxConstants.参数配置_本工厂厂名.getCode());
			paramCodes.add(WxConstants.参数配置_本工厂开户名称.getCode());
			paramCodes.add(WxConstants.参数配置_本工厂开户银行.getCode());
			paramCodes.add(WxConstants.参数配置_本工厂联系电话.getCode());
			paramCodes.add(WxConstants.参数配置_本工厂银行账号.getCode());
			paramCodes.add(WxConstants.参数配置_本工厂地址.getCode());
			List<WxSysConfig> list = iWxSysConfigService.queryWxSysConfigByCodes(paramCodes);
			for(WxSysConfig config : list ) {
				if(config.getParamCode().equals(WxConstants.参数配置_本工厂厂名.getCode())) {
					repairVehicle.setOurFactoryName(config.getParamVal());
				}else if(config.getParamCode().equals(WxConstants.参数配置_本工厂开户名称.getCode())) {
					repairVehicle.setOurFactoryAcName(config.getParamVal());
				}else if(config.getParamCode().equals(WxConstants.参数配置_本工厂开户银行.getCode())) {
					repairVehicle.setOurFactoryAcBank(config.getParamVal());
				}else if(config.getParamCode().equals(WxConstants.参数配置_本工厂联系电话.getCode())) {
					repairVehicle.setOurFactoryPhone(config.getParamVal());
				}else if(config.getParamCode().equals(WxConstants.参数配置_本工厂银行账号.getCode())) {
					repairVehicle.setOurFactoryAcNo(config.getParamVal());
				}else if(config.getParamCode().equals(WxConstants.参数配置_本工厂地址.getCode())) {
					repairVehicle.setOurFactoryAddress(config.getParamVal());
				}
			}
		}
	}
}

